package com.zhanbo.zhanbowymusic.main.featured;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;

import com.mwx.baselib.base.BaseBindingAdapter;
import com.mwx.baselib.base.BaseFragment;
import com.mwx.baselib.bean.MVListBean;
import com.mwx.baselib.bean.PageSongListBean;
import com.mwx.baselib.net.BaseResponses;
import com.mwx.baselib.net.DefaultObserver;
import com.mwx.baselib.net.NetworkManager;
import com.mwx.baselib.view.Divider;
import com.mwx.baselib.view.DividerItemDecoration;
import com.zhanbo.zhanbowymusic.R;
import com.zhanbo.zhanbowymusic.databinding.FragmentSingerMvListBinding;
import com.zhanbo.zhanbowymusic.main.featured.adapter.SingerMVListAdapter;
import com.zhanbo.zhanbowymusic.play.bean.PlayMVBean;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 歌手MV列表
 */
public class SingerMVFragment extends BaseFragment<FragmentSingerMvListBinding> {
    private SingerMVListAdapter adapter;

    @Override
    protected Object getLayout() {
        return R.layout.fragment_singer_mv_list;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        initData();
        initView();
        initListener();
    }

    @Override
    protected void initData() {
        adapter = new SingerMVListAdapter(_mActivity);
        binding.rvSingerMvList.setAdapter(adapter);
    }

    @Override
    protected void initView() {
        if (getArguments()==null){
            return;
        }
        Map<String,String> map = new HashMap<>();
        map.put("artistId",getArguments().getInt("id")+"");
        NetworkManager.getInstance().getApiService().getMVList(map)
                .subscribeOn(Schedulers.io())
                .compose(this.bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new DefaultObserver<BaseResponses<MVListBean>>(_mActivity) {
                    @Override
                    public void onSuccess(BaseResponses<MVListBean> listBaseResponses) {
                        binding.rvSingerMvList.setLayoutManager(new GridLayoutManager(_mActivity, 3));
                        DividerItemDecoration dividerItemDecoration= new DividerItemDecoration();
                        dividerItemDecoration.setDividerLookup(new DividerItemDecoration.DividerLookup() {
                            @Override
                            public Divider getVerticalDivider(int position) {
                                return new Divider.Builder()
                                        .size(20)
                                        .color(Color.TRANSPARENT)
                                        .build();
                            }

                            @Override
                            public Divider getHorizontalDivider(int position) {
                                return new Divider.Builder()
                                        .size(20)
                                        .color(Color.TRANSPARENT)
                                        .build();
                            }
                        });
                        binding.rvSingerMvList.addItemDecoration(dividerItemDecoration);

                        adapter.getItems().addAll(listBaseResponses.getData().getItems());
                        adapter.notifyDataSetChanged();
                        if (adapter.getItems().size()<1){
                            binding.rvSingerMvList.setVisibility(View.GONE);
                            binding.tvNoData.setVisibility(View.VISIBLE);
                        }else {
                            binding.tvNoData.setVisibility(View.GONE);
                            binding.rvSingerMvList.setVisibility(View.VISIBLE);
                        }

                    }
                });



    }

    @Override
    protected void initListener() {
        adapter.setOnItemClickListener(new BaseBindingAdapter.OnItemClickListener<MVListBean.ItemsDTO>() {
            @Override
            public void onItemClick(int position) {
                EventBus.getDefault().post(new PlayMVBean(adapter.getItems().get(position)));
            }
        });
    }

    public static SingerMVFragment newInstance(int singerId) {

        Bundle args = new Bundle();
        args.putInt("id",singerId);
        SingerMVFragment fragment = new SingerMVFragment();
        fragment.setArguments(args);
        return fragment;
    }
}
